import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {
	count: 10
}

const mutations = {
	increment (state) {
		state.count += 1
	},
	decrement (state) {
		state.count--
	}
}

const actions = {
	// 函数的参数肯定是对象
	increment: ({ commit }) => commit('increment'),
	decrement: ({ commit }) => commit('decrement'),
	incrementIfOdd ({ commit,state }) {
		if ((state.count + 1) % 2 === 0) {
			commit('increment')
		}
	},
	incrementAsync ({ commit }, amount) {
		console.log(amount)
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				commit('increment')
				resolve()
			}, 1000)
		})
	}
}

const getters = {
	evenOrOdd: state => state.count % 2 === 0 ? 'even' : 'odd'
}

const moduleA = {
	state,
	getters,
	actions,
	mutations
}

const moduleB = {
	state: {
		count: 'its moduleB',
	},
	// getters,
	actions,
	mutations
}

export default new Vuex.Store({
	modules: {
		a: moduleA,
		b: moduleB
	}
})